package fishBids.group3.behaviour.market;

import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.lang.Codec.CodecException;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import es.upm.agents.ontology.ToGetPrice;
import es.upm.agents.ontology.ToGetPriceResponse;
import fishBids.group3.AgMarket;
import fishBids.group3.common.Constants;

public class MarketGetPriceBehaviour extends CyclicBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2472621384661749131L;
	
	public MarketGetPriceBehaviour(AgMarket agent, Codec codec, Ontology ontology){
		this.agMarket = agent;
		this.codec =  codec;
		this.ontology = ontology;
		
	}

	@Override
	public void action() {

		// Waits for estimation requests
		// Receive the message 
		ACLMessage msg = agMarket.receive(MessageTemplate.MatchProtocol(Constants.GET_PRICE) ); 

		if (msg != null)

		{
			if (msg.getPerformative() == ACLMessage.QUERY_REF) {

					//System.out.println( "Market >> GetPrice QUERY_REF message arrived");
					ContentElement ce;
					try {
						ce = agMarket.getContentManager().extractContent(msg);

						if (ce instanceof Action) {
							Action ac = (Action) ce;
							Concept cpt = ac.getAction();
							if (cpt instanceof ToGetPrice) {
								ToGetPrice toGetPrice = (ToGetPrice) cpt;
//								System.out.println("Market>>>Get Price of Product: " + toGetPrice.getProductType().getType() );
								ToGetPriceResponse toGetPriceResponse = new ToGetPriceResponse();
								toGetPriceResponse.setProductType(toGetPrice.getProductType());
								toGetPriceResponse.setPrice((Float) this.agMarket.getItemsPrices().get(toGetPrice.getProductType().getType()));
								
								//TODO set the initial price in the market of the product, or calculate the price
								//product.setPrice(500);
								ACLMessage reply = msg.createReply();
								reply.setLanguage(codec.getName());
								reply.setOntology(ontology.getName());
								reply.setPerformative(ACLMessage.INFORM);
								Action agAction = new Action(myAgent.getAID(),toGetPriceResponse);								
								
								try {
									agMarket.getContentManager().fillContent(reply, agAction);
									//System.out.println( "Market >>  Ready to send getPrice INFORM nuevo " );
									agMarket.send(reply);
									return;
								} catch (CodecException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (OntologyException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}								

							}
						}
					} catch (UngroundedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (CodecException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (OntologyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}


				}
			
			ACLMessage reply = msg.createReply();
			reply.setLanguage(codec.getName());
			reply.setOntology(ontology.getName());
			reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
//			System.out.println("Market>>> GetPrice NOT_UNDERSTOOD message");
			agMarket.send(reply);
			return;

		}else{
			block();
		}



	}
	
	AgMarket agMarket;
	Codec codec;
	Ontology ontology;

}
